package com.blq.web.controller;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.blq.common.annotation.RepeatSubmit;
import com.blq.common.annotation.Log;
import com.blq.common.core.controller.BaseController;
import com.blq.common.core.domain.PageQuery;
import com.blq.common.core.domain.R;
import com.blq.common.core.validate.AddGroup;
import com.blq.common.core.validate.EditGroup;
import com.blq.common.enums.BusinessType;
import com.blq.common.utils.poi.ExcelUtil;
import com.blq.web.domain.vo.UserMoneyItemVo;
import com.blq.web.domain.bo.UserMoneyItemBo;
import com.blq.web.service.IUserMoneyItemService;
import com.blq.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 钱包明细Controller
 *
 * @author zeh
 * @date 2023-06-25
 */
@Validated
@Api(value = "钱包明细控制器", tags = {"钱包明细管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/moneyItem")
public class UserMoneyItemController extends BaseController {

    private final IUserMoneyItemService iUserMoneyItemService;

    /**
     * 查询钱包明细列表
     */
    @ApiOperation("查询钱包明细列表")
//    @SaCheckPermission("web:moneyItem:list")
    @GetMapping("/list")
    public TableDataInfo<UserMoneyItemVo> list(UserMoneyItemBo bo, PageQuery pageQuery) {
        return iUserMoneyItemService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询提现或分账到银行卡待支付列表
     */
    @ApiOperation("查询提现到银行卡待支付列表")
//    @SaCheckPermission("web:moneyItem:list")
    @GetMapping("/cashBankList")
    public TableDataInfo<UserMoneyItemVo> cashBankList(UserMoneyItemBo bo, PageQuery pageQuery) {
        return iUserMoneyItemService.queryPageBankList(bo, pageQuery);
    }

    /**
     * 处理提现或分账到银行卡条目
     */
    @ApiOperation("处理银行卡提现")
    @SaCheckPermission("web:moneyItem:edit")
    @Log(title = "处理银行卡提现或分账", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/handleBankCash")
    public R<Void> handleBankCash(@Validated(EditGroup.class) @RequestBody UserMoneyItemBo bo) {
        return toAjax(iUserMoneyItemService.handleBankCash(bo) ? 1 : 0);
    }

    /**
     * 导出钱包明细列表
     */
    @ApiOperation("导出钱包明细列表")
    @SaCheckPermission("web:moneyItem:export")
    @Log(title = "钱包明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserMoneyItemBo bo, HttpServletResponse response) {
        List<UserMoneyItemVo> list = iUserMoneyItemService.queryList(bo);
        ExcelUtil.exportExcel(list, "钱包明细", UserMoneyItemVo.class, response);
    }

    /**
     * 获取钱包明细详细信息
     */
    @ApiOperation("获取钱包明细详细信息")
//    @SaCheckPermission("web:moneyItem:query")
    @GetMapping("/{itemId}")
    public R<UserMoneyItemVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("itemId") Long itemId) {
        return R.ok(iUserMoneyItemService.queryById(itemId));
    }

    /**
     * 新增钱包明细
     */
    @ApiOperation("新增钱包明细")
    @SaCheckPermission("web:moneyItem:add")
    @Log(title = "钱包明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserMoneyItemBo bo) {
        return toAjax(iUserMoneyItemService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改钱包明细
     */
    @ApiOperation("修改钱包明细")
    @SaCheckPermission("web:moneyItem:edit")
    @Log(title = "钱包明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserMoneyItemBo bo) {
        return toAjax(iUserMoneyItemService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除钱包明细
     */
    @ApiOperation("删除钱包明细")
    @SaCheckPermission("web:moneyItem:remove")
    @Log(title = "钱包明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] itemIds) {
        return toAjax(iUserMoneyItemService.deleteWithValidByIds(Arrays.asList(itemIds), true) ? 1 : 0);
    }

    /**
     * 查询对象总收益和冻结
     */
    @ApiOperation("查询对象总收益和冻结")
    @GetMapping("/totalAndFreeze")
    public R<JSONObject> totalAndFreeze(Long objId) {
        return R.ok(iUserMoneyItemService.queryTotalAndFreeze(objId));
    }

    @ApiOperation("零钱提现")
    @PostMapping("/cashMoneys")
    @RepeatSubmit()
    @Log(title = "零钱提现", businessType = BusinessType.INSERT)
    public R<Void> cashMoneys(@RequestBody UserMoneyItemBo bo) {
        return toAjax(iUserMoneyItemService.cashMoneys(bo.getObjId(), bo.getMoney(), bo.getClassify()));
    }
}
